import React, { PureComponent, createRef } from "react";


class Counter extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      counter: 0
    }
  }

  render() {
    return (
      <div>
        <h2>当前计数: {this.state.counter}</h2>
        <button onClick={e => this.increment()}>+1</button>
      </div>
    )
  }

  increment() {
    this.setState({
      counter: this.state.counter + 1
    })
  }
}

export default class App extends PureComponent {
  constructor() {
    super()
    this.titleRef = createRef()
    this.counterRef = createRef()
    this.titleEl = null
    this.state = {}
  }
  render() {
    return (
      <div>
        <h2 ref="titleRef">Hello React</h2>
        {/* 目前React推荐的方式 */}
        <h2 ref={this.titleRef}>Hello React</h2>
        <h2 ref={arg => this.titleEl = arg}>Hello React</h2>
        <button onClick={e => this.changeText()}>改变文本</button>
        <hr />
        <Counter ref={this.counterRef} />
        <button onClick={e => this.appBtnClick()}>App按钮</button>
      </div>
    )
  }
  changeText() {
    // 1.使用方式一: 字符串(不推荐, 后续的更新会删除)
    this.refs.titleRef.innerHTML = "Hello Coder"
    this.titleRef.current.increment = "Hello JavaScript"
    this.titleEl.innerHTML = "Hello TypeScript"
  }
}

